package com.watson.jpa.result;

import lombok.Data;

/**
 * @Description: TODO
 * @Author watson
 * @Date 2022/8/10 10:39
 * @Version 1.0
 */
@Data
public class ObjectRestResponse<T> {
    private int code=200;
    private String msg;
    private T data;

    public ObjectRestResponse(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ObjectRestResponse(String message, T data) {
        this.msg = message;
        this.data = data;
    }

    public ObjectRestResponse(String message) {
        this.msg = message;
    }

    public ObjectRestResponse(T data) {
        this.data = data;
    }

    public ObjectRestResponse() {
    }

    /**
     * 返回成功消息
     *
     * @return 成功消息
     */
    public static ObjectRestResponse success() {
        return ObjectRestResponse.success("操作成功");
    }

    /**
     * 返回成功数据
     *
     * @return 成功消息
     */
    public static ObjectRestResponse success(Object data) {
        return ObjectRestResponse.success("操作成功", data);
    }

    /**
     * 返回成功消息
     *
     * @param msg 返回内容
     * @return 成功消息
     */
    public static ObjectRestResponse success(String msg) {
        return ObjectRestResponse.success(msg, null);
    }

    /**
     * 返回成功消息
     *
     * @param msg  返回内容
     * @param data 数据对象
     * @return 成功消息
     */
    public static ObjectRestResponse success(String msg, Object data) {
        return new ObjectRestResponse(200, msg, data);
    }

    /**
     * 返回错误消息
     *
     * @return
     */
    public static ObjectRestResponse error() {
        return ObjectRestResponse.error("操作失败");
    }

    /**
     * 返回错误消息
     *
     * @param msg 返回内容
     * @return 警告消息
     */
    public static ObjectRestResponse error(String msg) {
        return ObjectRestResponse.error(msg, null);
    }

    /**
     * 返回错误消息
     *
     * @param msg  返回内容
     * @param data 数据对象
     * @return 警告消息
     */
    public static ObjectRestResponse error(String msg, Object data) {
        return new ObjectRestResponse(500, msg, data);
    }

    /**
     * 返回错误消息
     *
     * @param code 状态码
     * @param msg  返回内容
     * @return 警告消息
     */
    public static ObjectRestResponse error(int code, String msg) {
        return new ObjectRestResponse(code, msg, null);
    }


    public ObjectRestResponse code(int rel) {
        this.setCode(rel);
        return this;
    }

    public ObjectRestResponse data(T data) {
        this.setData(data);
        return this;
    }
    public ObjectRestResponse msg(String msg) {
        this.setMsg(msg);
        return this;
    }
}
